package com.flnet.spsys.controller;
import com.flnet.spsys.entity.User;
import com.flnet.spsys.entity.VO.UserLoginVO;
import com.flnet.spsys.service.UserService;
import com.flnet.spsys.utils.Md5Util;
import com.flnet.spsys.utils.ResultJson;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 *
 */

@Api(tags = "用户接口")
@RestController
@RequestMapping("/main/v1/user")
@CrossOrigin
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @ApiOperation(value = "用户注册", notes = "用户注册")
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @CrossOrigin
    @ApiResponses(
            @ApiResponse(code = 200, message = "操作成功", response = User.class)
    )
    public Map<String, Object> register(@RequestParam String userName,
                                        @RequestParam String passwd) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(passwd)) {
            map.put("msg", "用户名和密码不能为空");
            return map;
        }
        // 验证用户名是否已经注册
        User exsitUser = userService.selectByName(userName);
        if (exsitUser != null) {
            map.put("msg", "该用户名已存在!");
            return map;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(new Date());
        User user = new User();
        user.setName(userName);
        //user.setRegistTime(format);
        //user.setStatus("1");
        user.setPassword(Md5Util.MD5(passwd) + userName);
        int count = userService.insert(user);
        System.out.println(count);
        if (count != 1) {
            map.put("msg", "注册失败");
            return map;
        }
        map.put("msg", "注册成功");
        return map;
    }
    @ApiOperation(value = "用户登录", notes = "用户登录")
    @ApiResponses(
            @ApiResponse(code = 200, message = "操作成功", response = ResultJson.class)
    )
    @ResponseBody
    @PostMapping(value = "/login")
    @CrossOrigin
    public Object login(@RequestBody UserLoginVO userLoginVO) {
        String username = userLoginVO.getUsername();
        String password = userLoginVO.getPasswd();

        User user = userService.selectByName(username);
        if (user == null) {
            return ResultJson.failure("该用户未注册",null);
        }
        if (!password.equals("123456")) {
            return ResultJson.failure("密码错误,请重新输入",null);
        }
//        if (!user.getPasswd().equals(Md5Util.MD5(passwd) + username)) {
//            return ResultJson.failure("密码错误,请重新输入",null);
//        }
        //session.setAttribute("loginUser", user);
        return ResultJson.success(null);
    }

    @ApiOperation(value = "用户信息",notes = "获取用户信息")
    @ResponseBody
    @PostMapping("/info")
    @CrossOrigin
    public Object getUserInfo(Integer id){
        User user = userService.selectByPrimaryKey(1);
        if(user == null){
            return ResultJson.failure("空信息",null);
        }
        return ResultJson.success(user);
    }



}
